import Tab from '../tab/tab.vue'
import RuleAdd from '../rule-add/rule-add.vue'
import { showMsg, setMenuType, getPromoterRuleList, deletePromoterRule } from '../../../../vuex/action'

export default {
  name: 'promoter-list',
  components: {
    'vue-tab': Tab,
    'vue-rule-add': RuleAdd
  },
  data() {
    return {
      list: []
    }
  },
  vuex: {
    getters: {
      fresh: state => state.promoterRuleListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xSetMenuType: setMenuType,
      xGetList: getPromoterRuleList,
      xDelete: deletePromoterRule
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList()
  },
  methods: {
    getList() {
      this.xGetList({})
        .then((data) => {
          this.list = data.rules
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList()
      }
    }
  }
}
